package com.ycsd.invest.order.business.opplugin;

import com.alibaba.dubbo.common.utils.StringUtils;
import kd.bos.coderule.api.CodeRuleInfo;
import kd.bos.context.RequestContext;
import kd.bos.dataentity.OperateOption;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.ExtendedDataEntity;
import kd.bos.entity.operate.result.OperationResult;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.AddValidatorsEventArgs;
import kd.bos.entity.plugin.args.BeforeOperationArgs;
import kd.bos.entity.validate.AbstractValidator;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.script.annotations.KSObject;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.QueryServiceHelper;
import kd.bos.servicehelper.coderule.CodeRuleServiceHelper;
import kd.bos.servicehelper.operation.DeleteServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;
import kd.sdk.annotation.SdkService;

import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;

public class StudentOp extends AbstractOperationServicePlugIn {

    @Override
    public void onAddValidators(AddValidatorsEventArgs e) {
        e.addValidator(new AbstractValidator() {
            @Override
            public void validate() {
                for (ExtendedDataEntity dataEntity : dataEntities) {
                    String name = (String) dataEntity.getValue("name");
                    if (StringUtils.isBlank(name) && name.length() < 2) {
                        this.addErrorMessage(dataEntity, "名字不能为空并且必须大于两个字符");
                    }
                }
            }
        });


//        });


        //加载单个实体
        DynamicObject obj = BusinessDataServiceHelper.loadSingle("2219166789802153984", "ycsd_student_info");
        System.out.println("------------------------------------------" + obj.getString("name"));
        //查询数据
        DynamicObjectCollection query = QueryServiceHelper.query("ycsd_student_info", "name,age,sex", new QFilter[]{new QFilter("name", QCP.like, "%5%")}, "age desc,name desc", 10);
        for (DynamicObject dynamicObject : query) {
            System.out.println("qqqqqqqqqqqqqq=====--02=6950=296=279=237============"+dynamicObject.toString());
        }
//        //删除数据
        DeleteServiceHelper deleteServiceHelper = new DeleteServiceHelper();
        OperationResult delete = deleteServiceHelper.deleteOperate("delete","ycsd_student_info", new String[]{"2218848606947924992"}, OperateOption.create());
        System.out.println("==============================="+delete.toString());

        //插入
        DynamicObject dynamicObject = BusinessDataServiceHelper.newDynamicObject("ycsd_student_info");
        dynamicObject.set("name", "dddd1");
        CodeRuleInfo codeRule = CodeRuleServiceHelper.getCodeRule(dynamicObject.getDataEntityType().getName(), dynamicObject, String.valueOf(RequestContext.get().getOrgId()));
        String number = CodeRuleServiceHelper.getNumber(codeRule, dynamicObject);
        dynamicObject.set("billno", number);
        OperationResult operationResult = SaveServiceHelper.saveOperate("save", dynamicObject.getDataEntityType().getName(), new DynamicObject[]{dynamicObject}, OperateOption.create());
        Object[] save = SaveServiceHelper.save(new DynamicObject[]{dynamicObject}, OperateOption.create());
        System.out.println("===============================" + save.toString());


        //更新
//        DynamicObject dynamicObject1 = BusinessDataServiceHelper.newDynamicObject("ycsd_student_info");
//
//
//        dynamicObject1.set("name", "修改");
//        dynamicObject1.set("id", "2220581489995689984");
//        CodeRuleInfo codeRule = CodeRuleServiceHelper.getCodeRule(dynamicObject1.getDataEntityType().getName(), dynamicObject1, String.valueOf(RequestContext.get().getOrgId()));
//        String number = CodeRuleServiceHelper.getNumber(codeRule, dynamicObject1);
//        dynamicObject1.set("billno", number);
//        SaveServiceHelper.update(dynamicObject1);
    }

    @Override
    public void beforeExecuteOperationTransaction(BeforeOperationArgs e) {
        super.beforeExecuteOperationTransaction(e);
        e.setCancel(true);
        e.setCancelMessage("取消后续操作");
    }
}
